package com.lsk.util;



import org.apache.commons.lang.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品海报生成工具类.
 */
public class ImageUtil {
    // 背景参数
    private String backgroundPath;
    private int backgroundWith;
    private int backgroundHeight;

    // 公司参数
    private String logoPath;
    private int logoX;
    private int logoY;
    private int logoWith;
    private int logoHeight;

    // 商户参数
    // 商户logo
    private String shopLogoPath;
    private int shopLogoX;
    private int shopLogoY;
    private int shopLogoWith;
    private int shopLogoHeight;
    // 商户名称
    private String shopName;
    private int shopNameX;
    private int shopNameY;
    private int shopNameWith;
    private int shopNameHeight;
    // 商户二维码
    private String qrCodePath;
    private int qrCodeX;
    private int qrCodeY;
    private int qrCodeWith;
    private int qrCodeHeight;

    // 商品参数
    // 商品图片
    private String goodsPicturePath;
    private int goodsPictureX;
    private int goodsPictureY;
    private int goodsPictureWith;
    private int goodsPictureHeight;
    // 商品描述
    private String goodsDescription;
    private int goodsDescriptionX;
    private int goodsDescriptionY;
    private int goodsDescriptionWith;
    private int goodsDescriptionHeight;

    //已销售
    private String sellDescription;
    private int sellDescriptionX;
    private int sellDescriptionY;
    private int sellDescriptionWith;
    private int sellDescriptionHeight;
    private String sellFont;
    private Color sellColor;


    //原价
    private String originalMoneyDescription;
    private int originalMoneyDescriptionX;
    private int originalMoneyDescriptionY;
    private int originalMoneyDescriptionWith;
    private int originalMoneyDescriptionHeight;
    private String originalMoneyFont;
    private Color originalMoneyColor;

    //当前价格
    private String moneyDescription;
    private int moneyDescriptionX;
    private int moneyDescriptionY;
    private int moneyDescriptionWith;
    private int moneyDescriptionHeight;
    private String moneyFont;
    private Color moneyColor;

    //分享者名字
    private String shareDescription;
    private int shareDescriptionX;
    private int shareDescriptionY;
    private int shareDescriptionWith;
    private int shareDescriptionHeight;

    public void setShareFontSize(int shareFontSize) {
        this.shareFontSize = shareFontSize;
    }

    private int shareFontSize;
    private Color shareColor;

    public String getShareDescription() {
        return shareDescription;
    }

    public void setShareDescription(String shareDescription) {
        this.shareDescription = shareDescription;
    }

    public int getShareDescriptionX() {
        return shareDescriptionX;
    }

    public void setShareDescriptionX(int shareDescriptionX) {
        this.shareDescriptionX = shareDescriptionX;
    }

    public int getShareDescriptionY() {
        return shareDescriptionY;
    }

    public void setShareDescriptionY(int shareDescriptionY) {
        this.shareDescriptionY = shareDescriptionY;
    }

    public int getShareDescriptionWith() {
        return shareDescriptionWith;
    }

    public void setShareDescriptionWith(int shareDescriptionWith) {
        this.shareDescriptionWith = shareDescriptionWith;
    }

    public int getShareDescriptionHeight() {
        return shareDescriptionHeight;
    }

    public void setShareDescriptionHeight(int shareDescriptionHeight) {
        this.shareDescriptionHeight = shareDescriptionHeight;
    }

    public int getShareFontSize() {
        return shareFontSize;
    }

    public void setShareFont(int shareFont) {
        this.shareFontSize = shareFont;
    }

    public Color getShareColor() {
        return shareColor;
    }

    public void setShareColor(Color shareColor) {
        this.shareColor = shareColor;
    }

    // 字体参数
    private String font;

    public String getSellFont() {
        return sellFont;
    }

    public void setSellFont(String sellFont) {
        this.sellFont = sellFont;
    }

    public Color getSellColor() {
        return sellColor;
    }

    public void setSellColor(Color sellColor) {
        this.sellColor = sellColor;
    }

    public String getOriginalMoneyFont() {
        return originalMoneyFont;
    }

    public void setOriginalMoneyFont(String originalMoneyFont) {
        this.originalMoneyFont = originalMoneyFont;
    }

    public Color getOriginalMoneyColor() {
        return originalMoneyColor;
    }

    public void setOriginalMoneyColor(Color originalMoneyColor) {
        this.originalMoneyColor = originalMoneyColor;
    }

    public String getMoneyFont() {
        return moneyFont;
    }

    public void setMoneyFont(String moneyFont) {
        this.moneyFont = moneyFont;
    }

    public Color getMoneyColor() {
        return moneyColor;
    }

    public void setMoneyColor(Color moneyColor) {
        this.moneyColor = moneyColor;
    }

    private int fontSize;

    // 图片信息
    private String pictureType;
    private String urlPath;


    public String getSellDescription() {
        return sellDescription;
    }

    public void setSellDescription(String sellDescription) {
        this.sellDescription = sellDescription;
    }

    public int getSellDescriptionX() {
        return sellDescriptionX;
    }

    public void setSellDescriptionX(int sellDescriptionX) {
        this.sellDescriptionX = sellDescriptionX;
    }

    public int getSellDescriptionY() {
        return sellDescriptionY;
    }

    public void setSellDescriptionY(int sellDescriptionY) {
        this.sellDescriptionY = sellDescriptionY;
    }

    public int getSellDescriptionWith() {
        return sellDescriptionWith;
    }

    public void setSellDescriptionWith(int sellDescriptionWith) {
        this.sellDescriptionWith = sellDescriptionWith;
    }

    public int getSellDescriptionHeight() {
        return sellDescriptionHeight;
    }

    public void setSellDescriptionHeight(int sellDescriptionHeight) {
        this.sellDescriptionHeight = sellDescriptionHeight;
    }

    public String getOriginalMoneyDescription() {
        return originalMoneyDescription;
    }

    public void setOriginalMoneyDescription(String originalMoneyDescription) {
        this.originalMoneyDescription = originalMoneyDescription;
    }

    public int getOriginalMoneyDescriptionX() {
        return originalMoneyDescriptionX;
    }

    public void setOriginalMoneyDescriptionX(int originalMoneyDescriptionX) {
        this.originalMoneyDescriptionX = originalMoneyDescriptionX;
    }

    public int getOriginalMoneyDescriptionY() {
        return originalMoneyDescriptionY;
    }

    public void setOriginalMoneyDescriptionY(int originalMoneyDescriptionY) {
        this.originalMoneyDescriptionY = originalMoneyDescriptionY;
    }

    public int getOriginalMoneyDescriptionWith() {
        return originalMoneyDescriptionWith;
    }

    public void setOriginalMoneyDescriptionWith(int originalMoneyDescriptionWith) {
        this.originalMoneyDescriptionWith = originalMoneyDescriptionWith;
    }

    public int getOriginalMoneyDescriptionHeight() {
        return originalMoneyDescriptionHeight;
    }

    public void setOriginalMoneyDescriptionHeight(int originalMoneyDescriptionHeight) {
        this.originalMoneyDescriptionHeight = originalMoneyDescriptionHeight;
    }

    public String getMoneyDescription() {
        return moneyDescription;
    }

    public void setMoneyDescription(String moneyDescription) {
        this.moneyDescription = moneyDescription;
    }

    public int getMoneyDescriptionX() {
        return moneyDescriptionX;
    }

    public void setMoneyDescriptionX(int moneyDescriptionX) {
        this.moneyDescriptionX = moneyDescriptionX;
    }

    public int getMoneyDescriptionY() {
        return moneyDescriptionY;
    }

    public void setMoneyDescriptionY(int moneyDescriptionY) {
        this.moneyDescriptionY = moneyDescriptionY;
    }

    public int getMoneyDescriptionWith() {
        return moneyDescriptionWith;
    }

    public void setMoneyDescriptionWith(int moneyDescriptionWith) {
        this.moneyDescriptionWith = moneyDescriptionWith;
    }

    public int getMoneyDescriptionHeight() {
        return moneyDescriptionHeight;
    }

    public void setMoneyDescriptionHeight(int moneyDescriptionHeight) {
        this.moneyDescriptionHeight = moneyDescriptionHeight;
    }




    public String getBackgroundPath() {
        return backgroundPath;
    }

    public void setBackgroundPath(String backgroundPath) {
        this.backgroundPath = backgroundPath;
    }

    public int getBackgroundWith() {
        return backgroundWith;
    }

    public void setBackgroundWith(int backgroundWith) {
        this.backgroundWith = backgroundWith;
    }

    public int getBackgroundHeight() {
        return backgroundHeight;
    }

    public void setBackgroundHeight(int backgroundHeight) {
        this.backgroundHeight = backgroundHeight;
    }

    public String getLogoPath() {
        return logoPath;
    }

    public void setLogoPath(String logoPath) {
        this.logoPath = logoPath;
    }

    public int getLogoX() {
        return logoX;
    }

    public void setLogoX(int logoX) {
        this.logoX = logoX;
    }

    public int getLogoY() {
        return logoY;
    }

    public void setLogoY(int logoY) {
        this.logoY = logoY;
    }

    public int getLogoWith() {
        return logoWith;
    }

    public void setLogoWith(int logoWith) {
        this.logoWith = logoWith;
    }

    public int getLogoHeight() {
        return logoHeight;
    }

    public void setLogoHeight(int logoHeight) {
        this.logoHeight = logoHeight;
    }

    public String getShopLogoPath() {
        return shopLogoPath;
    }

    public void setShopLogoPath(String shopLogoPath) {
        this.shopLogoPath = shopLogoPath;
    }

    public int getShopLogoX() {
        return shopLogoX;
    }

    public void setShopLogoX(int shopLogoX) {
        this.shopLogoX = shopLogoX;
    }

    public int getShopLogoY() {
        return shopLogoY;
    }

    public void setShopLogoY(int shopLogoY) {
        this.shopLogoY = shopLogoY;
    }

    public int getShopLogoWith() {
        return shopLogoWith;
    }

    public void setShopLogoWith(int shopLogoWith) {
        this.shopLogoWith = shopLogoWith;
    }

    public int getShopLogoHeight() {
        return shopLogoHeight;
    }

    public void setShopLogoHeight(int shopLogoHeight) {
        this.shopLogoHeight = shopLogoHeight;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public int getShopNameX() {
        return shopNameX;
    }

    public void setShopNameX(int shopNameX) {
        this.shopNameX = shopNameX;
    }

    public int getShopNameY() {
        return shopNameY;
    }

    public void setShopNameY(int shopNameY) {
        this.shopNameY = shopNameY;
    }

    public int getShopNameWith() {
        return shopNameWith;
    }

    public void setShopNameWith(int shopNameWith) {
        this.shopNameWith = shopNameWith;
    }

    public int getShopNameHeight() {
        return shopNameHeight;
    }

    public void setShopNameHeight(int shopNameHeight) {
        this.shopNameHeight = shopNameHeight;
    }

    public String getQrCodePath() {
        return qrCodePath;
    }

    public void setQrCodePath(String qrCodePath) {
        this.qrCodePath = qrCodePath;
    }

    public int getQrCodeX() {
        return qrCodeX;
    }

    public void setQrCodeX(int qrCodeX) {
        this.qrCodeX = qrCodeX;
    }

    public int getQrCodeY() {
        return qrCodeY;
    }

    public void setQrCodeY(int qrCodeY) {
        this.qrCodeY = qrCodeY;
    }

    public int getQrCodeWith() {
        return qrCodeWith;
    }

    public void setQrCodeWith(int qrCodeWith) {
        this.qrCodeWith = qrCodeWith;
    }

    public int getQrCodeHeight() {
        return qrCodeHeight;
    }

    public void setQrCodeHeight(int qrCodeHeight) {
        this.qrCodeHeight = qrCodeHeight;
    }

    public String getGoodsPicturePath() {
        return goodsPicturePath;
    }

    public void setGoodsPicturePath(String goodsPicturePath) {
        this.goodsPicturePath = goodsPicturePath;
    }

    public int getGoodsPictureX() {
        return goodsPictureX;
    }

    public void setGoodsPictureX(int goodsPictureX) {
        this.goodsPictureX = goodsPictureX;
    }

    public int getGoodsPictureY() {
        return goodsPictureY;
    }

    public void setGoodsPictureY(int goodsPictureY) {
        this.goodsPictureY = goodsPictureY;
    }

    public int getGoodsPictureWith() {
        return goodsPictureWith;
    }

    public void setGoodsPictureWith(int goodsPictureWith) {
        this.goodsPictureWith = goodsPictureWith;
    }

    public int getGoodsPictureHeight() {
        return goodsPictureHeight;
    }

    public void setGoodsPictureHeight(int goodsPictureHeight) {
        this.goodsPictureHeight = goodsPictureHeight;
    }

    public String getGoodsDescription() {
        return goodsDescription;
    }

    public void setGoodsDescription(String goodsDescription) {
        this.goodsDescription = goodsDescription;
    }

    public int getGoodsDescriptionX() {
        return goodsDescriptionX;
    }

    public void setGoodsDescriptionX(int goodsDescriptionX) {
        this.goodsDescriptionX = goodsDescriptionX;
    }

    public int getGoodsDescriptionY() {
        return goodsDescriptionY;
    }

    public void setGoodsDescriptionY(int goodsDescriptionY) {
        this.goodsDescriptionY = goodsDescriptionY;
    }

    public int getGoodsDescriptionWith() {
        return goodsDescriptionWith;
    }

    public void setGoodsDescriptionWith(int goodsDescriptionWith) {
        this.goodsDescriptionWith = goodsDescriptionWith;
    }

    public int getGoodsDescriptionHeight() {
        return goodsDescriptionHeight;
    }

    public void setGoodsDescriptionHeight(int goodsDescriptionHeight) {
        this.goodsDescriptionHeight = goodsDescriptionHeight;
    }

    public String getFont() {
        return font;
    }

    public void setFont(String font) {
        this.font = font;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public String getPictureType() {
        return pictureType;
    }

    public void setPictureType(String pictureType) {
        this.pictureType = pictureType;
    }

    public String getUrlPath() {
        return urlPath;
    }

    public void setUrlPath(String urlPath) {
        this.urlPath = urlPath;
    }


    /**
     * 画布初始化.
     *
     * @param path   路径
     * @param with   背景图宽度
     * @param height 背景图高度
     * @return canvas
     * @throws Exception
     */
    public static BufferedImage drawInit(String path, int with, int height) throws Exception {
        URL url = new URL(path);
        BufferedImage canvas = ImageIO.read(url);
        canvas = changeSize(canvas, with, height);
        return canvas;
    }

    /**
     * 对图片进行强制放大或缩小
     *
     * @param originalImage 原始图片
     * @return canvas
     */
    public static BufferedImage changeSize(BufferedImage originalImage, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, originalImage.getType());
        Graphics2D g2d = image.createGraphics();
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.OPAQUE);
        g2d = image.createGraphics();
        g2d.drawImage(originalImage, 0, 0, width, height, null);
        g2d.dispose();
        return image;
    }

    /**
     * 绘制公司信息.
     *
     * @param canvas 画布
     * @param logo   公司logo
     * @param logoX  logo横坐标
     * @param logoY  logo纵坐标
     * @return canvas
     */
    public static BufferedImage drawLogo(BufferedImage canvas, BufferedImage logo, int logoX, int logoY) {
        Graphics2D g2d = canvas.createGraphics();
        //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(logo, logoX, logoY, null);
        g2d.dispose();
        return canvas;
    }

    /**
     * 绘制商户信息.
     *
     * @param canvas         画布
     * @param shopLogo       店铺logo
     * @param shopLogoX      店铺logo横坐标
     * @param shopLogoY      店铺logo纵坐标
     * @param shopName       店铺名称
     * @param shopNameX      店铺名称横坐标
     * @param shopNameY      店铺名称纵坐标
     * @param shopNameWith   店铺名称宽度
     * @param shopNameHeight 店铺名称高度
     * @param qrCode         二维码
     * @param qrCodeX        二维码横坐标
     * @param qrCodeY        二维码纵坐标
     * @return canvas
     */
    public static BufferedImage drawBusiness(BufferedImage canvas, BufferedImage shopLogo, int shopLogoX, int shopLogoY,
                                             String shopName, int shopNameX, int shopNameY, int shopNameWith, int shopNameHeight,
                                             String font, int fontSize,
                                             BufferedImage qrCode, int qrCodeX, int qrCodeY,Color color) {
        Graphics2D g2d = drawPicAndWords(canvas, shopLogo, shopLogoX, shopLogoY, shopName, shopNameX, shopNameY,
                shopNameWith, shopNameHeight, font, fontSize, color);
        g2d.drawImage(qrCode, qrCodeX, qrCodeY, null);
        return canvas;
    }

    /**
     * 绘制商品信息.
     *
     * @param canvas                 画布
     * @param goodsPicture           商品图片
     * @param goodsPictureX          商品图片横坐标
     * @param goodsPictureY          商品图片纵坐标
     * @param goodsDescription       商品文字
     * @param goodsDescriptionX      商品文字横坐标
     * @param goodsDescriptionY      商品文字纵坐标
     * @param goodsDescriptionWith   商品文字宽度
     * @param goodsDescriptionHeight 商品文字高度
     * @param font                   字体
     * @param fontSize               字体大小
     * @return canvas
     */
    public static BufferedImage drawGoods(BufferedImage canvas, BufferedImage goodsPicture, int goodsPictureX, int goodsPictureY,
                                          String goodsDescription, int goodsDescriptionX, int goodsDescriptionY, int goodsDescriptionWith, int goodsDescriptionHeight,
                                          String font, int fontSize,Color color) {
        Graphics2D g2d = drawPicAndWords(canvas, goodsPicture, goodsPictureX, goodsPictureY,
                goodsDescription, goodsDescriptionX, goodsDescriptionY, goodsDescriptionWith, goodsDescriptionHeight,
                font, fontSize,color);
        return canvas;
    }

    /**
     * 绘制一个图和一个文字框.
     *
     * @param canvas      画布
     * @param image       图
     * @param imageX      图横坐标
     * @param imageY      图纵坐标
     * @param words       文字
     * @param wordsX      文字横坐标
     * @param wordsY      文字纵坐标
     * @param wordsWith   文字宽度
     * @param wordsHeight 文字高度
     * @param font        字体
     * @param fontSize    字体大小
     * @param fontSize    字体颜色
     * @return g2d
     */
    private static Graphics2D drawPicAndWords(BufferedImage canvas, BufferedImage image, int imageX, int imageY,
                                              String words, int wordsX, int wordsY, int wordsWith, int wordsHeight,
                                              String font, int fontSize,Color color) {
        Graphics2D g2d = canvas.createGraphics();
        g2d.drawImage(image, imageX, imageY, null);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.getDeviceConfiguration().createCompatibleImage(wordsWith, wordsHeight, Transparency.TRANSLUCENT);
        g2d.setFont(new Font(font, Font.PLAIN, fontSize));
        g2d.setColor(color);
        List<String> contents = new ArrayList();
        int length = words.length();
        double row = length * fontSize / wordsWith;
        int lineWordNum = (int) Math.ceil(length / row);
        if (length <= lineWordNum) {
            contents.add(words);
        } else {
            for (int i = 0; i < length; i += lineWordNum) {
                int end = i + lineWordNum;
                if (end > length) {
                    end = length;
                }
                contents.add(StringUtils.substring(words, i, end));
            }
        }
        for (int i = 0; i < contents.size(); i++) {
            String word = contents.get(i);
            if (i != 0) {
                wordsY += fontSize * 1.2;
            }
            g2d.drawString(word, wordsX, wordsY);
        }
        return g2d;
    }



    /**
     * 绘制二维码
     *
     * @param canvas      画布
     * @return g2d
     */
    private static BufferedImage drawQc(BufferedImage canvas,BufferedImage qrCode,
                                              int qrCodeX,  int qrCodeY, int qrCodeHeight,int qrCodeWidth) {
        Graphics2D g2d = canvas.createGraphics();

        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.getDeviceConfiguration().createCompatibleImage(qrCodeWidth, qrCodeHeight, Transparency.TRANSLUCENT);
        g2d.drawImage(qrCode, qrCodeX, qrCodeY, null);

        return canvas;
    }




    /**
     * 绘制文字信息
     */
    private static BufferedImage drawWords(BufferedImage canvas, String words, int wordsX, int wordsY,
                                        int wordsWith, int wordsHeight,
                                              String font, int fontSize,Color color) {
        Graphics2D g2d = canvas.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.getDeviceConfiguration().createCompatibleImage(wordsWith, wordsHeight, Transparency.TRANSLUCENT);
        g2d.setFont(new Font(font, Font.PLAIN, fontSize));
        g2d.setColor(color);
        List<String> contents = new ArrayList();
        int length = words.length();
        double row = length * fontSize / wordsWith;
        int lineWordNum = (int) Math.ceil(length / row);
        if (length <= lineWordNum) {
            contents.add(words);
        } else {
            for (int i = 0; i < length; i += lineWordNum) {
                int end = i + lineWordNum;
                if (end > length) {
                    end = length;
                }
                contents.add(StringUtils.substring(words, i, end));
            }
        }
        for (int i = 0; i < contents.size(); i++) {
            String word = contents.get(i);
            if (i != 0) {
                wordsY += fontSize * 1.2;
            }
            g2d.drawString(word, wordsX, wordsY);
        }
        return canvas;
    }

    /**
     * 现价绘制
     */
    private static BufferedImage drawMoneyWords(BufferedImage canvas, String words, int wordsX, int wordsY,
                                           int wordsWith, int wordsHeight,
                                           String font, int fontSize,Color color) {
        Graphics2D g2d = canvas.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.getDeviceConfiguration().createCompatibleImage(wordsWith, wordsHeight, Transparency.TRANSLUCENT);
        g2d.setFont(new Font(font, Font.BOLD, fontSize));
        g2d.setColor(color);
        List<String> contents = new ArrayList();
        int length = words.length();
        double row = length * fontSize / wordsWith;
        int lineWordNum = (int) Math.ceil(length / row);
        if (length <= lineWordNum) {
            contents.add(words);
        } else {
            for (int i = 0; i < length; i += lineWordNum) {
                int end = i + lineWordNum;
                if (end > length) {
                    end = length;
                }
                contents.add(StringUtils.substring(words, i, end));
            }
        }
        for (int i = 0; i < contents.size(); i++) {
            String word = contents.get(i);
            if (i != 0) {
                wordsY += fontSize * 1.2;
            }
            g2d.drawString(word, wordsX, wordsY);
        }
        return canvas;
    }


    /**
     * 保存图片.
     *
     * @param canvas  画布
     * @param type    图片类型
     * @param urlPath 路径
     * @return string
     * @throws Exception
     */
    public static String save(BufferedImage canvas, String type, String urlPath) throws Exception {
        ImageIO.write(canvas, type, new File(urlPath));
        String path = null;
        return path;
    }

    /**
     * 生成海报.
     *
     * @return
     */
    public static String getPoster(ImageUtil imageUtil) throws Exception {
        // 背景
        BufferedImage canvas = drawInit(imageUtil.getBackgroundPath(), imageUtil.getBackgroundWith(), imageUtil.getBackgroundHeight());
        // 公司信息
        //BufferedImage logo = ImageIO.read(new URL(imageUtil.getLogoPath()));
        //canvas = drawLogo(canvas, logo, imageUtil.getLogoX(), imageUtil.getLogoY());
        // 商户信息

//        BufferedImage shopLogo = drawInit(imageUtil.getShopLogoPath(), imageUtil.getShopLogoWith(), imageUtil.getShopLogoHeight());
        BufferedImage qrCode = drawInit(imageUtil.getQrCodePath(), imageUtil.getQrCodeWith(), imageUtil.getQrCodeHeight());
        canvas = drawQc(canvas,qrCode,imageUtil.getQrCodeX(),imageUtil.getQrCodeY()
        ,imageUtil.getQrCodeHeight(),imageUtil.getQrCodeWith());


        // 商品信息
        BufferedImage goodsPicture = drawInit(imageUtil.getGoodsPicturePath(), imageUtil.getGoodsPictureWith(), imageUtil.getGoodsPictureHeight());

        canvas = drawGoods(canvas, goodsPicture, imageUtil.getGoodsPictureX(), imageUtil.getGoodsPictureY(),
                imageUtil.getGoodsDescription(), imageUtil.getGoodsDescriptionX(), imageUtil.getGoodsDescriptionY(), imageUtil.getGoodsDescriptionWith(), imageUtil.getGoodsDescriptionHeight(),
                imageUtil.getFont(), imageUtil.getFontSize(),Color.BLACK);
        //售卖

        canvas = drawWords(canvas, imageUtil.getSellDescription(),
                imageUtil.getSellDescriptionX(), imageUtil.getSellDescriptionY(),
                imageUtil.getSellDescriptionWith(), imageUtil.getSellDescriptionHeight(),
                imageUtil.getFont(), 30,Color.GRAY);
        //原价
        canvas =drawWords(canvas, imageUtil.getOriginalMoneyDescription(),
                imageUtil.getOriginalMoneyDescriptionX(), imageUtil.getOriginalMoneyDescriptionY(),
                imageUtil.getOriginalMoneyDescriptionWith(), imageUtil.getOriginalMoneyDescriptionHeight(),
                imageUtil.getFont(), 20,imageUtil.getOriginalMoneyColor());

        //现价
        canvas = drawMoneyWords(canvas, imageUtil.getMoneyDescription(),
                imageUtil.getMoneyDescriptionX(), imageUtil.getMoneyDescriptionY(),
                imageUtil.getMoneyDescriptionWith(), imageUtil.getMoneyDescriptionHeight(),
                imageUtil.getMoneyFont(), 40,imageUtil.getMoneyColor());
        //分销者logo

            canvas = drawMoneyWords(canvas, imageUtil.getShareDescription(),
                    imageUtil.getShareDescriptionX(), imageUtil.getShareDescriptionY(),
                    imageUtil.getShareDescriptionWith(), imageUtil.getShareDescriptionHeight(),
                    imageUtil.getMoneyFont()+"", imageUtil.getShareFontSize(),imageUtil.getShareColor());



        save(canvas, imageUtil.getPictureType(), imageUtil.getUrlPath());
        return null;
    }

    /**
     * test.
     *
     * @param args args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        String baseUrl="http://qiniu.keleziyouxing.com/base.jpg";

        // 生成海报
        ImageUtil imageUtil = new ImageUtil();
        imageUtil.setBackgroundPath(baseUrl);
        imageUtil.setBackgroundWith(750);
        imageUtil.setBackgroundHeight(1100);

        imageUtil.setLogoPath("");
        imageUtil.setLogoX(800);
        imageUtil.setLogoY(0);
//        imageUtil.setLogoWith();
//        imageUtil.setLogoHeight();

        imageUtil.setShopLogoPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553763241787&di=81ed9ec823de36398d97f88f2a3efa02&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F12%2F38%2F54%2F27y58PICXCf.jpg");
        imageUtil.setShopLogoX(50);
        imageUtil.setShopLogoY(50);
        imageUtil.setShopLogoWith(150);
        imageUtil.setShopLogoHeight(150);


        /*二维码*/
        imageUtil.setQrCodePath("http://www.ccoalnews.com/pic/201804/16/72078895-85a8-490b-af38-055e8bdffe14_zsize.jpg");
        imageUtil.setQrCodeX(500);
        imageUtil.setQrCodeY(790);
        imageUtil.setQrCodeWith(200);
        imageUtil.setQrCodeHeight(200);

        /*渲染商品图片*/
        imageUtil.setGoodsPicturePath("http://ps7jny53c.bkt.clouddn.com//logo/d26154cdccb145abb4ae35d3c6de96c5");
        imageUtil.setGoodsPictureX(10);
        imageUtil.setGoodsPictureY(50);
        imageUtil.setGoodsPictureWith(740);
        imageUtil.setGoodsPictureHeight(720);
        /*商品描述*/
        imageUtil.setGoodsDescription("泰国金枕头，来自大自然的馈赠！特价88元，欲购从速！");
        imageUtil.setGoodsDescriptionX(10);
        imageUtil.setGoodsDescriptionY(820);
        imageUtil.setGoodsDescriptionWith(350);
        imageUtil.setGoodsDescriptionHeight(100);
        imageUtil.setFont("微软雅黑");
        imageUtil.setFontSize(30);
        /*售卖*/
        imageUtil.setSellDescription("已售999件");
        imageUtil.setSellDescriptionX(10);
        imageUtil.setSellDescriptionY(930);
        imageUtil.setSellDescriptionWith(300);
        imageUtil.setSellDescriptionHeight(300);
        imageUtil.setSellColor(Color.GRAY);
        /*原价*/
        imageUtil.setOriginalMoneyDescription("原价 ￥859");
        imageUtil.setOriginalMoneyDescriptionX(140);
        imageUtil.setOriginalMoneyDescriptionY(1000);
        imageUtil.setOriginalMoneyDescriptionHeight(300);
        imageUtil.setOriginalMoneyDescriptionWith(300);
        imageUtil.setOriginalMoneyColor(Color.GRAY);
        /*现价*/
        imageUtil.setMoneyDescription("￥500");
        imageUtil.setMoneyDescriptionX(10);
        imageUtil.setMoneyDescriptionY(1000);
        imageUtil.setMoneyDescriptionHeight(300);
        imageUtil.setMoneyDescriptionWith(300);
        imageUtil.setMoneyColor(Color.red);
        imageUtil.setPictureType("jpg");

        imageUtil.setUrlPath("C:\\poster.jpg");
        getPoster(imageUtil);
    }

    /**
     * 生成宣传海报
     * @param qrCodePath 二维码路径
     * @param goodsPicturePath 商品图片路径
     * @param goodsDescription 商品描述    商品标题
     * @param sellDescription  售价描述  已售99件
     * @param originalMoneyDescription 原价 ￥100
     * @param moneyDescription 现价  ￥50
     * @param resultPath  保存的图片路径
     */
    public void createPoster(String qrCodePath,String goodsPicturePath,String goodsDescription
                ,String sellDescription,String originalMoneyDescription,String moneyDescription,String resultPath,String shareDescription){
        String baseUrl="http://lvyouimg.dingyuankeji.shop/base.jpg";

        // 生成海报
        ImageUtil imageUtil = new ImageUtil();
        imageUtil.setBackgroundPath(baseUrl);
        imageUtil.setBackgroundWith(750);
        imageUtil.setBackgroundHeight(1260);

        imageUtil.setLogoPath("");
        imageUtil.setLogoX(800);
        imageUtil.setLogoY(0);
//        imageUtil.setLogoWith();
//        imageUtil.setLogoHeight();

        imageUtil.setShopLogoPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553763241787&di=81ed9ec823de36398d97f88f2a3efa02&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F12%2F38%2F54%2F27y58PICXCf.jpg");
        imageUtil.setShopLogoX(50);
        imageUtil.setShopLogoY(50);
        imageUtil.setShopLogoWith(150);
        imageUtil.setShopLogoHeight(150);


        /*二维码*/
        imageUtil.setQrCodePath(qrCodePath);
        imageUtil.setQrCodeX(450);
        imageUtil.setQrCodeY(990);
        imageUtil.setQrCodeWith(280);
        imageUtil.setQrCodeHeight(280);

        /*渲染商品图片*/
        imageUtil.setGoodsPicturePath(goodsPicturePath);
        imageUtil.setGoodsPictureX(0);
        imageUtil.setGoodsPictureY(0);
        imageUtil.setGoodsPictureWith(750);
        imageUtil.setGoodsPictureHeight(1000);
        /*商品描述*/
        imageUtil.setGoodsDescription(goodsDescription);
        imageUtil.setGoodsDescriptionX(30);
        imageUtil.setGoodsDescriptionY(1050);
        imageUtil.setGoodsDescriptionWith(350);
        imageUtil.setGoodsDescriptionHeight(100);
        imageUtil.setFont("微软雅黑");
        imageUtil.setFontSize(40);
        /*售卖*/
        imageUtil.setSellDescription(sellDescription);
        imageUtil.setSellDescriptionX(30);
        imageUtil.setSellDescriptionY(1150);
        imageUtil.setSellDescriptionWith(300);
        imageUtil.setSellDescriptionHeight(300);
        imageUtil.setSellColor(Color.GRAY);
        /*原价*/
        imageUtil.setOriginalMoneyDescription(originalMoneyDescription);
        imageUtil.setOriginalMoneyDescriptionX(180);
        imageUtil.setOriginalMoneyDescriptionY(1200);
        imageUtil.setOriginalMoneyDescriptionHeight(300);
        imageUtil.setOriginalMoneyDescriptionWith(300);
        imageUtil.setOriginalMoneyColor(Color.GRAY);
        /*现价*/
        imageUtil.setMoneyDescription(moneyDescription);
        imageUtil.setMoneyDescriptionX(30);
        imageUtil.setMoneyDescriptionY(1200);
        imageUtil.setMoneyDescriptionHeight(300);
        imageUtil.setMoneyDescriptionWith(300);
        imageUtil.setMoneyColor(Color.red);
        imageUtil.setPictureType("jpg");


        //分销者logo
        imageUtil.setShareDescription(shareDescription);
        imageUtil.setShareDescriptionX(130);
        imageUtil.setShareDescriptionY(1250);
        imageUtil.setShareDescriptionWith(200);
        imageUtil.setShareDescriptionHeight(100);
        imageUtil.setShareColor(Color.GRAY);
        imageUtil.setShareFontSize(30);
        imageUtil.setUrlPath(resultPath);


        try {
            getPoster(imageUtil);
        }catch (Exception e){
            Syslog.error(e);
        }

    }


}
